<template>
  <el-dialog class="import-loading-dialog" v-bind="$attrs" :show-close="false" :close-on-click-modal="false" :close-on-press-escape="false" append-to-body @close="handleClose" v-on="$listeners" @open="handleOpen">
    <div class="text-center">
      {{ $attrs.title }}<br>
      <div class="my-2 font-bold">{{ fileName }}</div>
      <div class="progress">
        <div id="dynamic" class="progress-bar progress-bar-striped active" :class="{'progress-bar--success': progress >= 100, 'progress-bar--error': failed}" role="progressbar" aria-valuenow="100" aria-valuemin="0" aria-valuemax="100" :style="{width: progress + '%'}">
          {{ progress }} %
        </div>
      </div>
    </div>
    <div v-if="failed" class="text-danger text-center mb-4">
      Failed, <a class="underline hover:underline" @click.prevent="downloadErrorReport"><i class="el-icon-download align-middle" />Download error report</a>
    </div>
    <div class="text-center">
      <el-button v-if="progress < 100" type="danger" size="small" @click="uploadCancel">
        {{ $t('flyparam.task_cancel') }}
      </el-button>
    </div>
  </el-dialog>
</template>

<script>
import { getParameterTemplateInfoByPackageName } from '@/api/parameter'
import { getTaskProgress } from '@/api/upg-task'
import { getDownloadUrl } from '@/api/app'

export default {
  props: ['taskUuid', 'appPackageName', 'fileName', 'estimateTime'],
  data() {
    return {
      progress: 0,
      failed: false
    }
  },
  methods: {
    uploadCancel() {
      clearInterval(this.$requestInterval)
      clearInterval(this.$progressInterval)
      this.$emit('update:model-value', false)
      this.$emit('cancel')
      this.resId = ''
    },
    handleClose() {
      Object.assign(this.$data, this.$options.data())
    },
    handleOpen() {
      this.$progressInterval = setInterval(() => {
        if (this.progress >= 99) {
          clearInterval(this.$progressInterval)
          return
        }
        this.progress += Number((100 / 28).toFixed(2))
      }, 2000)
      this.$requestInterval = setInterval(() => {
        getTaskProgress(this.taskUuid).then(res => {
          if (res.status === 1) {
            this.$message.success('Imported Successfully.')
            clearInterval(this.$requestInterval)
            this.progress = 100
            getParameterTemplateInfoByPackageName(this.appPackageName).then((res) => {
              this.$router.push(`/flyparameter/config/${res.id}?unsaved=true`)
            })
            return
          } else if (res.status === 3) {
            this.$message.error(this.$t('flyparam.import_failed'))
            this.errorReportUrl = res.downloadUrl
            this.failed = true
            clearInterval(this.$requestInterval)
            clearInterval(this.$progressInterval)
          }
        })
      }, 5000)
    },
    downloadErrorReport() {
      getDownloadUrl(this.errorReportUrl).then(res => {
        window.location.href = res.url
      })
    }
  }

}
</script>

<style>
.import-loading-dialog.el-dialog__wrapper .el-dialog .el-dialog__body {
  padding: 30px 20px;
}
</style>

<style lang="scss">

.progress {
  margin: 10px;
  width: 700px;
  height: 20px;
  margin-left: auto;
  margin-right: auto;
  margin-bottom: 20px;
  overflow: hidden;
  background-color: #f5f5f5;
  border-radius: 4px;
  -webkit-box-shadow: inset 0 1px 2px rgba(0,0,0,.1);
  box-shadow: inset 0 1px 2px rgba(0,0,0,.1);
  .progress-bar {
    background-image: linear-gradient(45deg,rgba(255,255,255,.15) 25%,transparent 25%,transparent 50%,rgba(255,255,255,.15) 50%,rgba(255,255,255,.15) 75%,transparent 75%,transparent);
    -webkit-background-size: 40px 40px;
    background-size: 40px 40px;
    animation: progress-bar-stripes 2s linear infinite;
    float: left;
    width: 0;
    height: 100%;
    font-size: 12px;
    line-height: 20px;
    color: #fff;
    text-align: center;
    background-color: #337ab7;
    -webkit-box-shadow: inset 0 -1px 0 rgba(0,0,0,.15);
    box-shadow: inset 0 -1px 0 rgba(0,0,0,.15);
    -webkit-transition: width .6s ease;
    -o-transition: width .6s ease;
    transition: width .6s ease;

    &--success {
      background-color: #5cb85c;
    }

    &--error {
      @apply bg-red-700;
    }

    @keyframes progress-bar-stripes {
      0% {
        background-position: 40px 0;
      }

      100% {
          background-position: 0 0;
      }
    }

  }
}

</style>
